package com.yu.convertor;

import org.apache.commons.beanutils.Converter;

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

public class DateTimeConvertor implements Converter {
    //常用的几种日期类型的字符串
    private static final String DATE = "yyyy-MM-dd";//长度为10
    private static final String DATETIME = "yyyy-MM-dd HH:mm";//长度为16
    private static final String DATETIME1 = "yyyy-MM-dd HH:mm:ss";//长度为19
    private static final String TIMESTAMP = "yyyy-MM-dd HH:mm:ss.SSS";//长度为23

    //把o转换为aClass类型，此处是将String类型转换位Date类型
    @Override
    public <T> T convert(Class<T> aClass, Object o) {
        return toDate(aClass,o);
    }
    public <T> T toDate(Class<T> type, Object value) {
        System.out.println("634yghi4n9petgy298345iutwgnj45yerhgovq35uihgrquibg4hgq4ui2");
        //如果value值为空或为空字符串的话，返回null
        if(value==null||"".equals(value)) {
            return null;
        }
        //判断value值是否为String类型
        if(value instanceof String) {
            String dateValue = value.toString().trim();//trim()作用研究一下
            //获取时间类型字符串长度
            int length = dateValue.length();
            //判断别转换的类型是否为时间类型

            if(type.equals(Date.class)) {
                try {
                    SimpleDateFormat simpleDateFormat = null;
                    if(length<=10) {
                        simpleDateFormat = new SimpleDateFormat(DATE, new DateFormatSymbols(Locale.CHINA));
                    }else if(length<=16) {
                        simpleDateFormat = new SimpleDateFormat(DATETIME, new DateFormatSymbols(Locale.CHINA));
                    }else if(length<=19) {
                        simpleDateFormat = new SimpleDateFormat(DATETIME1, new DateFormatSymbols(Locale.CHINA));
                    }else if(length<=23) {
                        simpleDateFormat = new SimpleDateFormat(TIMESTAMP, new DateFormatSymbols(Locale.CHINA));
                    }
                    Date date = simpleDateFormat.parse(dateValue);
                    return (T) date;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if(type.equals(LocalDateTime.class)) {
                DateTimeFormatter fmt = null;
                //测试了一下
                if(length<=10) {
                    dateValue += " 00:00:00";
                    length += dateValue.length();
                }
                if (length <= 16) {
                    fmt = DateTimeFormatter.ofPattern(DATETIME);
                } else
                    // 转换为“yyyy-MM-dd HH:mm:ss”格式的Date对象
                    if (length <= 19) {
                        fmt = DateTimeFormatter.ofPattern(DATETIME1);
                    } else
                        // 转换为“yyyy-MM-dd HH:mm:ss.SSS”格式的Date对象
                        if (length <= 23) {
                            fmt = DateTimeFormatter.ofPattern(TIMESTAMP);
                        }
                return (T) LocalDateTime.parse(dateValue, fmt);
            }
        }
        return (T)value;
    }

    public static void main(String[] args) {
/*        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String str = "2019-12-12 12:12";
        LocalDateTime date = LocalDateTime.parse(str, fmt);
        System.out.println("String转LocalDate::" + date);*/
        String time = "2019-07-23";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(dateTime);
    }
}
